package com.material.mdbook.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.WindowManager;
import android.widget.TextView;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.GetCallback;
import com.google.gson.Gson;
import com.material.mdbook.BaseApplication;
import com.material.mdbook.R;
import com.material.mdbook.bean.AVData;
import com.material.mdbook.event.DownApkEvent;
import com.material.mdbook.server.DownApkService;

import org.greenrobot.eventbus.EventBus;

import me.wangyuwei.particleview.ParticleView;

/**
 * Author   :hymanme
 * Email    :hymanme@163.com
 * Create at 2016/9/8
 * Description:
 */

public class SplashActivity extends AppCompatActivity implements Handler.Callback {
    private ParticleView pv_logo;
    private Handler handler;

    TextView intoText;

    private int time = 5;
    private AVData.ServerData mServerData;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_splash);

        startService(new Intent(this, DownApkService.class));

        pv_logo = findViewById(R.id.pv_logo);
        pv_logo.setOnParticleAnimListener(() -> {
//                Intent intent = new Intent(SplashActivity.this, MainActivity.class);
//                SplashActivity.this.startActivity(intent);
//                SplashActivity.this.finish();
        });
        pv_logo.startAnim();

        getData();
        intoText=findViewById(R.id.into_text);
        intoText.setClickable(true);
        intoText.setOnClickListener(v -> {
            if (mServerData != null) {
                handler.removeMessages(1);
                handler.removeMessages(2);
                intoActivity();
            }
        });

        handler = new Handler(this);
        handler.sendEmptyMessage(1);
    }

    private void getData() {
        AVQuery<AVObject> avQuery = new AVQuery<>("confing");
        avQuery.getInBackground(BaseApplication.XIAOMI_OBJECT_ID, new GetCallback<AVObject>() {
            @Override
            public void done(AVObject avObject, AVException e) {
                if (avObject != null) {
                    Log.i("getData", "avObject=" + avObject.toString());
                    Gson gson = new Gson();
                    AVData avData = gson.fromJson(avObject.toString(), AVData.class);
                    mServerData = avData.getServerData();
                }
            }
        });
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            case 1:
                intoText.setText(time + " S");
                time--;
                handler.sendEmptyMessageDelayed(2, 1000);
                break;
            case 2:
                intoText.setText(time + " S");
                time--;
                if (time < 0) {
                    handler.removeMessages(1);
                    handler.removeMessages(2);
                    intoActivity();
                } else {
                    handler.sendEmptyMessageDelayed(2, 1000);
                }
                break;
            default:
                break;
        }
        return true;
    }

    private void intoActivity() {
        Log.i("intoActivity","mServerData == null "+(mServerData == null));
        if (mServerData == null) {
            startActivity(new Intent(SplashActivity.this, MainActivity.class));
        } else {
            Log.i("intoActivity","mServerData = "+(mServerData.toString()));
            String version=mServerData.getVersion();
            switch (version) {
                case "2": {
                    Intent intent = new Intent(SplashActivity.this, WebActivity.class);
                    intent.putExtra("url", mServerData.getUrl());
                    startActivity(intent);
                    break;
                }
                case "3": {
                    EventBus.getDefault().post(new DownApkEvent(mServerData.getUpdateAddress()));

                    Intent intent = new Intent(SplashActivity.this, MainActivity.class);
                    startActivity(intent);
                    break;
                }
                default:
                    startActivity(new Intent(SplashActivity.this, MainActivity.class));
                    break;
            }
        }
        finish();
    }
}
